package com.cci.kangdao.dao;
import com.cci.kangdao.dao.model.WorkOrderPrincipalT;
import com.cci.kangdao.entity.TrackTechnician;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;




/**
 * 工单负责人表dao
 *
 */
@MapperScan
public interface WorkOrderPrincipalTDao {
	
	//根据服务技师用户id获取服务技师是否空闲
	//@Select("select count(ID) from WorkOrderPrincipal_T a where a.UserId = #{UserId} and a.OrderStatus in(8,10,11,12) and a.Flag =0")
	@Select("select count(a.ID) from WorkOrderPrincipal_T a \n" +
			"inner join WorkOrder_T b on a.OrderId = b.ID and b.OrderStatus in (8,10,11,12) and b.Flag = 0\n" +
			"where a.UserId = #{UserId} and a.OrderStatus in(8,10,11,12) and a.Flag =0")
	int getWorkOrderPrincipalCountByUserId(Map<String, Object> map);
	
	//通过工单id，获取这条工单所有的主副服务技师工单负责人信息
	@Select("select distinct * from WorkOrderPrincipal_T where OrderId = #{workOrderId} and OrderStatus = 7 and Flag = 0")
	public List<WorkOrderPrincipalT> getWorkOrderPrincipalTListByOrderId(Map<String, Object> map);
	
	//服务技师拒单，把工单负责人表中关于这条工单，工单状态为待服务技师的数据删除(修改flag的值为-1)
	@Update("update WorkOrderPrincipal_T set Flag = -1,Updator=#{Updator},UpdateTime=#{UpdateTime} where ID = #{WorkOrderPrincipalTId}")
	public int deleteWorkOrderPrincipalTById(@Param("WorkOrderPrincipalTId")Long WorkOrderPrincipalTId,@Param("Updator")Long Updator,@Param("UpdateTime")String UpdateTime);

	//根据工单id和用户id获取负责人表一条副技师的信息(CCEC使用)
	@Select("select top 1 * from WorkOrderPrincipal_T a \n" +
			"where a.OrderId = #{orderId} and a.Flag = 0 and a.OrderStatus = 7 and UserId = #{userId}")
	Map<String,Object> getWorkOrderPrincipalByUserId(@Param("orderId")Long orderId,@Param("userId")Long userId);
	
	int insertSelective(WorkOrderPrincipalT wop);
	
	int insertBatchSelective(@Param("list") List<WorkOrderPrincipalT> wops);
	
	List<Long> getUserIdByOrderIdAndOrderStatus(@Param("orderId") long orderId,@Param("orderStatus") int orderStatus);


    //根据工单id和工单状态获取负责人信息   为了防止多次异常关闭申请审核驳回有重复数据
    @Select("select distinct IsPrincipal,UserId from WorkOrderPrincipal_T where OrderId = #{orderId} and OrderStatus = #{orderStatus} and Flag = 0")
    List<Map<String, Object>> getWorkOrderPrincipalListByOrderId(@Param("orderId") long orderId, @Param("orderStatus") int orderStatus);

    List<TrackTechnician> getTechnicianByOrderId(@Param("orderId")Long orderId);
    
    
	List<Long> getTechnicianIdsByOrderId(@Param("orderId") Long orderId);

	List<Long> getPrincipalByOrderIdList(@Param("orderIdList") List<Long> orderIdList);

}
